{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "8cf235de-fa13-4a79-823f-c26681e3f646",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from datetime import datetime\n",
    "import os\n",
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "90470366-90bc-49cf-aa29-c08ff7d9d9bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "path = r'D:\\MissionList\\Misssons'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "2d4d0f4f-3809-452d-8177-54f378e6801c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def menu():\n",
    "    print('1 创建新清单')\n",
    "    print('2 读取已有清单')\n",
    "    while True:\n",
    "        num = input('请输入(1-2)')\n",
    "        if num in ['1', '2']:\n",
    "            break\n",
    "            \n",
    "    if num == '1':\n",
    "        list = MissionList()\n",
    "        list.save_csv()\n",
    "        \n",
    "    elif num == '2':\n",
    "        pattern = r'\\.csv$'\n",
    "        while True:\n",
    "            count = input('请输入要读取的清单的编号')\n",
    "            # print('count',count)\n",
    "            folder = os.path.join(path, f'MissionList{count}')\n",
    "            # print('folder', folder)\n",
    "            if os.path.exists(folder):\n",
    "                break\n",
    "            print('编号不存在')\n",
    "        try:\n",
    "            files = os.listdir(folder)\n",
    "            for file in files:\n",
    "                if re.search(pattern, file):\n",
    "                    name = MissionList(path = os.path.join(folder, file), read_csv=True)\n",
    "                    name.show()\n",
    "                    print(f'清单{count}读取已完成')\n",
    "                    break\n",
    "        except Exception as e:\n",
    "            print(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "7f1ee28a-9a15-4110-92ca-d6df83063fa6",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "目前的思路：\n",
    "    1. 重要与紧急程度的表示：\n",
    "    正半轴：1 - 5\n",
    "    负半轴：-5 - -1\n",
    "    x轴表示重要程度，y轴表示紧急程度\n",
    "\n",
    "    值得一提的是，真正重要的，往往是重要且不紧急的\n",
    "\"\"\"\n",
    "# 需要添加方法，完成任务，修改任务内容，添加任务\n",
    "import pandas as pd\n",
    "\n",
    "class MissionList:\n",
    "    def __init__(self, path=r'MissionList.csv', read_json=False, read_csv=False):\n",
    "        if read_json:\n",
    "            self.list = self.read_json()\n",
    "        elif read_csv:\n",
    "            self.list = self.read_csv(path)\n",
    "        else:\n",
    "            self.list = self.init_list()\n",
    "        self.address = 'D:/MissionList/Misssons/'\n",
    "\n",
    "    def read_json(self, path):\n",
    "        pass\n",
    "\n",
    "    def read_csv(self, path):\n",
    "        try:\n",
    "            df = pd.read_csv(path, encoding='utf-8')\n",
    "            sorted_df = df.sort_values(by=['total', 'importance'], ascending=[False, False])\n",
    "            return sorted_df\n",
    "        except Exception as e:\n",
    "            print(e)\n",
    "\n",
    "    def save_csv(self):\n",
    "        try:\n",
    "            # 读取 mission_count\n",
    "            with open(r'D:\\MissionList\\Misssons\\control.txt', 'r') as f:\n",
    "                line = f.readline()\n",
    "                match = re.search(r'mission_count = (\\d+);', line)\n",
    "                mission_count = int(match.group(1))\n",
    "                \n",
    "            # 更新 mission_count\n",
    "            with open(r'D:\\MissionList\\Misssons\\control.txt', 'w') as f:\n",
    "                newline = f'mission_count = {str(mission_count + 1)};'\n",
    "                f.write(newline)\n",
    "                \n",
    "            #创建文件夹\n",
    "            path = os.path.join(self.address, f'MissionList{match.group(1)}')\n",
    "            os.makedirs(path, exist_ok=True)\n",
    "            current_time = datetime.now().strftime(\"%Y-%m-%d %H时%M分.csv\")\n",
    "            self.list.to_csv(os.path.join(path, current_time), encoding='utf-8', index = False)\n",
    "        except Exception as e:\n",
    "            print(e)\n",
    "\n",
    "    def get_num(self, flag):\n",
    "        name = '重要性' if flag else '紧急性'\n",
    "        while True:\n",
    "            try:\n",
    "                num = int(input(f'{name}(-5-5):'))\n",
    "                if num not in range(-5, 6):\n",
    "                    print('重新输入')\n",
    "                    continue\n",
    "                return num\n",
    "            except Exception as e:\n",
    "                print(e, '\\n重新输入')\n",
    "                continue\n",
    "\n",
    "    def init_list(self):\n",
    "        missions = []\n",
    "        count = 1\n",
    "        while True:\n",
    "            print('任务', count)\n",
    "            mission = input('任务(0 to stop):')\n",
    "            if mission == '0':\n",
    "                break\n",
    "            urgency = self.get_num(0)\n",
    "            importance = self.get_num(1)\n",
    "            total = urgency + importance\n",
    "            missions.append({'mission': mission, 'importance': importance, 'urgency': urgency, 'total': total})\n",
    "            count += 1\n",
    "        df = pd.DataFrame(missions)\n",
    "        sorted_df = df.sort_values(by=['total', 'importance'], ascending=[False, False])\n",
    "        print('任务清单已创建')\n",
    "        return sorted_df\n",
    "\n",
    "    def show(self):\n",
    "        print(f'{\"任务\".ljust(6)} | {\"任务名称\".ljust(25)} | {\"重要性\".ljust(8)} | {\"紧急性\".ljust(8)}')\n",
    "        for index, mission in self.list.iterrows():\n",
    "            print(f'{str(index+1).ljust(6)} | {mission[\"mission\"].ljust(25)} | {str(mission[\"importance\"]).ljust(8)} | {str(mission[\"urgency\"]).ljust(8)}')\n",
    "    # 问题: 打印始终无法对齐？？？    \n",
    "    def finish_mission(self):\n",
    "        try:\n",
    "            self.show()\n",
    "            index = int(input('所完成任务的编号'))\n",
    "            if index not in self.list.index.tolist():\n",
    "                raise ValueError('编号不存在')\n",
    "            self.list.drop(index,inplace=True)\n",
    "            print('任务', index, '已完成')\n",
    "            self.save_csv()\n",
    "            return True\n",
    "        except Exception as e:\n",
    "            print(e)\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "c8051655-9d05-4541-a87d-8114dd8c261c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 创建新清单\n",
      "2 读取已有清单\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入(1-2) 2\n",
      "请输入要读取的清单的编号 1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "任务     | 任务名称                      | 重要性      | 紧急性     \n",
      "1      | 比特学习一节课                   | 5        | 3       \n",
      "2      | 数据结构继续学习                  | 5        | 2       \n",
      "3      | 导论project中期任务\t            | 4        | 3       \n",
      "清单1读取已完成\n"
     ]
    }
   ],
   "source": [
    "menu()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "e1f145c3-044c-416d-a25f-228b577a75d3",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'name' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mNameError\u001b[39m                                 Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[55]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mname\u001b[49m.list\n",
      "\u001b[31mNameError\u001b[39m: name 'name' is not defined"
     ]
    }
   ],
   "source": [
    "name.list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "4951abe2-4b9f-4883-8686-ee155024954f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 创建新清单\n",
      "2 读取已有清单\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入(1-2) 1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "任务 1\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "任务(0 to stop): 比特学习一节课\n",
      "紧急性(-5-5): 3\n",
      "重要性(-5-5): 5\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "任务 2\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "任务(0 to stop): 晚12点看车万\n",
      "紧急性(-5-5): 0\n",
      "重要性(-5-5): 5\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "任务 3\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "任务(0 to stop): 0\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "任务清单已创建\n"
     ]
    }
   ],
   "source": [
    "menu()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "05e95760-f401-4a9d-bb87-bbab31115ee8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 创建新清单\n",
      "2 读取已有清单\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入(1-2) 2\n",
      "请输入要读取的清单的编号 0\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "count 0\n",
      "folder D:\\MissionList\\Misssons\\MissionList0\n",
      "编号不存在\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入要读取的清单的编号 3\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "count 3\n",
      "folder D:\\MissionList\\Misssons\\MissionList3\n",
      "编号不存在\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入要读取的清单的编号 4\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "count 4\n",
      "folder D:\\MissionList\\Misssons\\MissionList4\n",
      "编号不存在\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入要读取的清单的编号 2\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "count 2\n",
      "folder D:\\MissionList\\Misssons\\MissionList2\n",
      "任务     | 任务名称                      | 重要性      | 紧急性     \n",
      "1      | 比特学习一节课                   | 5        | 3       \n",
      "2      | 晚12点看车万                   | 5        | 0       \n",
      "清单2读取已完成\n"
     ]
    }
   ],
   "source": [
    "menu()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:miniconda3]",
   "language": "python",
   "name": "conda-env-miniconda3-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
